package service;

import client.ManageClientConnectServiceThread;
import common.Message;
import common.MessageType;
import common.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class QQService {
    private ServerSocket serverSocket;
    public static HashMap<String,User> userHashMap = new HashMap<>();
    static
    {
        userHashMap.put("100",new User("100","123456"));
        userHashMap.put("陈克",new User("陈克","123456"));
        userHashMap.put("gs",new User("gs","123456"));
    }

    public  QQService() {
        try {
            serverSocket = new ServerSocket(9999);
            while (true)
            {
                Socket socket = serverSocket.accept();
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                User user = (User)objectInputStream.readObject();
                //临时验证账号密码
                Message message = new Message();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                if (checkUser(user.getUserId(),user.getPasswd()))
                {
                    message.setMesType(MessageType.MESSAGE_LOGIN_SUCCEED);
                    objectOutputStream.writeObject(message);
                    //启动socket线程，保持链接
                    ServiceConnectClientThread serviceConnectClientThread = new ServiceConnectClientThread(socket, user.getUserId());
                    serviceConnectClientThread.start();
                    //登录成功判断是否存在未读的消息
                    checkUnread(user.getUserId(),socket);
                    //存到map中统一管理
                    ManageServiceConnectClientThread.addThreadMap(user.getUserId(),serviceConnectClientThread);
                    System.out.println("当前连接数量："+ ManageServiceConnectClientThread.stringServiceConnectClientThreadHashMap.size());

                }else
                {
                    message.setMesType(MessageType.MESSAGE_LOGIN_FAIL);
                    objectOutputStream.writeObject(message);
                    socket.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public boolean checkUser(String userId,String pwd)
    {
        User user = userHashMap.get(userId);
        if (user==null)
        {
            return false;
        }
        if (!user.getPasswd().equals(pwd))
        {
            return false;
        }
        //校验一下单点登录
        ServiceConnectClientThread threadMap = ManageServiceConnectClientThread.getThreadMap(userId);
        if (threadMap !=null)
        {
            return false;
        }
        return true;
    }
    //登录成功判断是否存在未读的消息,没有掠过，有就将消息发送到服务
    public void checkUnread(String userId,Socket socket)
    {
        if (MangeMessage.hasMessage(userId))
        {
            ArrayList<Message> message = MangeMessage.getMessage(userId);
            Iterator<Message> iterator = message.iterator();
            while (iterator.hasNext()) {
                Message next =  iterator.next();
                try {
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(next);
                    //发送完移除
                    iterator.remove();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
